from flask import Flask
from flask import request
from flask import render_template

# 接受一个参数，构建web服务对象
app = Flask(__name__, template_folder='templates')


# 申明web服务访问的资源,所有框架都要继承GWSI接口
# 装饰器，将路由映射到视图函数index
# restful 风格动作有'get', 'post', 'delete', 'put', 'patch'
# mvc =>mtc 模板引擎
# java spring 框架：m =model v=view c=control
# python mtv ： m=model t=templates v=view 和控制器
@app.route('/get', methods=['GET', 'POST'])
def get():
    # requst (httprequst)请求对象，请求参数，请求方法，请求头，表单数据，文件信息等
    if request.method == 'GET':
        return """
        <h1>用户登陆界面</h1>
        <form action="/get"  method="POST">
          账号:<br>
          <input name='name' type='text'>
          密码：<br>
          <input name='pwd'  type='text'>
          <br>
          <input type='submit' name='submit' >
         </form>
        """
    else:
        name = request.form.get('name')
        pwd = request.form.get('pwd')
        if all((name == 'tianchuan',
                pwd == '123456'
                )):
            return """
            <h1 style='color:blue'>登录成功</h1>
            """
        else:
            return """
                <h1 style='color:orange'>登录失败 
                <a href='/get'>重试</a>
                </h1>
                """


@app.route('/login', methods=['GET'])
def login():
    return """
    <h1>用户登陆界面</h1>
        <form action='/get' method='POST'>
         账号:<br>
        <input type="text" name="name">
        <br>
         密码:<br>
        <input type="text" name="pwd">
        <br>
        <input type='submit' name='提交'>
        </form> 
    """


# MTV 模型+模板+视图
@app.route('/bank', methods=['GET', 'POST'])
def bank():
    data = {'name': 'tianchuan',
            'error': '卡号和密码不正确！'}
    return render_template('mybank.html', **data)


app.run(host='localhost', port=5000, debug=True)
